package cs342.uwosh.group1.mycontacts;

import java.util.ArrayList;

import com.mycontacts.R;
import com.mycontacts.R.id;
import com.mycontacts.R.layout;
import com.mycontacts.R.menu;

import cs342.uwosh.group1.mycontacts.Threads.DeleteChosenContactsTask;
import cs342.uwosh.group1.mycontacts.Threads.GetAllContactsChooserTask;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class DeleteContactsChooser extends Activity {

	public static ListView listOfDeleteContacts;
	public static ArrayAdapter<Contact> deletecontactListAdapter;
	public static int sizeOfAdapter;
	public static Context mainContext;

	
	ArrayList<Contact> contactsToDelete = new ArrayList<Contact>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_delete_contacts_chooser);
		
		mainContext = this;
		

		listOfDeleteContacts = (ListView) findViewById(R.id.deleteContactsListView);
		
		new GetAllContactsChooserTask().execute();
		
		listOfDeleteContacts.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
			{
				Contact clickedContact = deletecontactListAdapter.getItem(position);
				CheckBox addToCatChkBox = (CheckBox)view.findViewById(R.id.deleteContactCheckBox);
				addToCatChkBox.setChecked(!addToCatChkBox.isChecked());
				if(contactsToDelete.contains(clickedContact))
				{
					contactsToDelete.remove(clickedContact);
					MainActivity.changed = true;
				}
				else
				{
					contactsToDelete.add(clickedContact);
				}
			}	
		});
	}
	
	public void cancel(View view)
	{
		this.finish();
	}
	
	@SuppressWarnings("unchecked")
	public void accept(View view)
	{
		new DeleteChosenContactsTask().execute(contactsToDelete);
		this.finish();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater()
				.inflate(R.menu.activity_delete_contacts_chooser, menu);
		return true;
	}

}
